import request from "@/utils/request";
import { jsonToPath } from "@/utils/common";
import type {
  SkuData,
  SkuDataResp,
  SkuDataPageResp,
  SkuDataDetailResp,
} from "./type";
import type { BasePageReq } from "@/api/baseType";

enum API {
  //保存sku
  SAVE_SKU = "/admin/product/saveSkuInfo",
  //根据Spu获取sku列表
  LIST_SKU_BY_SPU = "/admin/product/findBySpuId",
  //分页获取sku列表
  LIST_SKU = "/admin/product/list",
  //上架
  ON_SALE = "/admin/product/onSale",
  //下架
  CANCEL_SALE = "/admin/product/cancelSale",
  //删除
  DELETE_SKU = "/admin/product/deleteSku",
  //获取sku详细信息
  SKU_INFO = "/admin/product/getSkuInfo",
}

//保存sku
export const saveSku = (data: SkuData) => {
  return request.post<any, any>(API.SAVE_SKU, data);
};

//获取sku列表
export const listSkuBySpu = (spuId: number | undefined) => {
  return request.get<any, SkuDataResp>(API.LIST_SKU_BY_SPU + `/${spuId}`);
};

//分页获取sku列表
export const listSku = (param: BasePageReq) => {
  return request.get<any, SkuDataPageResp>(API.LIST_SKU + jsonToPath(param));
};

//上下架
export const changeSale = (skuId: number | undefined, type: number) => {
  if (type === 1) {
    return request.get<any, any>(API.ON_SALE + `/${skuId}`);
  }
  return request.get<any, any>(API.CANCEL_SALE + `/${skuId}`);
};

//删除sku
export const deleteSku = (skuId: number | undefined) => {
  return request.delete<any, any>(API.DELETE_SKU + `/${skuId}`);
};

//获取sku详细信息
export const getSkuInfo = (skuId: number | undefined) => {
  return request.get<any, SkuDataDetailResp>(API.SKU_INFO + `/${skuId}`);
};
